﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using mapSpliter;

namespace WindowsApplication1
{
    public partial class PrintForm : Form
    {
        public PrintForm()
        {
            InitializeComponent();
        }

        class NameAndGeometry
        {
            private IGeometry envelope;
            private string name;

            public NameAndGeometry(string name, IGeometry envelope)
            {
                this.name = name;
                this.envelope = envelope;
            }

            public IGeometry Geometry
            {
                get { return envelope; }
            }

            public override string ToString()
            {
                return name;
            }
        }

        private Hashtable jiedao2shequ = new Hashtable();

        public AxMapControl MapControl
        {
         set
         {
             if (mapcontrol != null)
                 return;
             mapcontrol = value;
             int count = mapcontrol.LayerCount;
             IFeatureLayer jiedaolayer = null;
             IFeatureLayer shequlayer = null;
             for(int i = 0; i < count; i++)
             {
                 ILayer layer = mapcontrol.get_Layer(i);
                 if (layer.Name == ConfigurationManager.AppSettings["街道图层"])
                 {
                     jiedaolayer = layer as IFeatureLayer;                     
                 }                 
                 else if (layer.Name == ConfigurationManager.AppSettings["社区图层"])
                 {
                     shequlayer = layer as IFeatureLayer;
                 }
             }
             if(jiedaolayer != null && shequlayer != null)
             {
                 IFeatureClass fclass = jiedaolayer.FeatureClass;
                 IGeoDataset geods = fclass as IGeoDataset;
                 NameAndGeometry ne1 = new NameAndGeometry("全部街道", geods.Extent);
                 jiedaocomboBox1.Items.Add(ne1);
                 List<NameAndGeometry> allshequ = new List<NameAndGeometry>();
                 jiedao2shequ.Add(ne1.ToString(), allshequ);
                 
                 //int 
                 IQueryFilter qfilter = new QueryFilterClass();
                 IFeatureCursor fcursor = jiedaolayer.Search(qfilter, true);
                 IFeature feature = fcursor.NextFeature();
                 string jiedaoname = ConfigurationManager.AppSettings["街道名称"];
                 string shequname = ConfigurationManager.AppSettings["社区名称"];
                 int jiedaonameindex = fclass.FindField(jiedaoname);
                 int shequnameindex = shequlayer.FeatureClass.FindField(shequname);
                 while (feature != null)
                 {
                     string namevalue = feature.get_Value(jiedaonameindex).ToString();
                     IEnvelope env = feature.Extent;
                     NameAndGeometry ne = new NameAndGeometry(namevalue, feature.ShapeCopy);
                     jiedaocomboBox1.Items.Add(ne);
                     ISpatialFilter sfilter = new SpatialFilterClass();
                     sfilter.Geometry = feature.Shape;
                     sfilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
                     IFeatureCursor cursor1 = shequlayer.Search(sfilter, true);
                     IFeature feature1 = cursor1.NextFeature();
                     List<NameAndGeometry> shequs = new List<NameAndGeometry>();
                     while(feature1 != null)
                     {
                         shequs.Add(new NameAndGeometry(feature1.get_Value(shequnameindex).ToString(), feature1.ShapeCopy));
                         feature1 = cursor1.NextFeature();
                     }
                     jiedao2shequ.Add(namevalue,shequs);
                     Marshal.ReleaseComObject(cursor1);
                     allshequ.AddRange(shequs);
                     feature = fcursor.NextFeature();
                 }
                 Marshal.ReleaseComObject(fcursor);
             }
         }
        }

        private AxMapControl mapcontrol;
        

        private void LocateForm_Load(object sender, EventArgs e)
        {
            
        }

        private void jiedaocomboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (jiedaocomboBox1.SelectedItem == null)
                return;
            NameAndGeometry ne = jiedaocomboBox1.SelectedItem as NameAndGeometry;
            shequcomboBox2.Items.Clear();
            shequcomboBox2.Items.AddRange((jiedao2shequ[ne.ToString()] as List<NameAndGeometry>).ToArray());
        }

        private void shequcomboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (shequcomboBox2.SelectedItem == null)
                return;
            NameAndGeometry ne = shequcomboBox2.SelectedItem as NameAndGeometry;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (shequcomboBox2.SelectedItem == null)
            {
                MessageBox.Show("您没有选择要打印的社区!");
                return;
            }
            NameAndGeometry ne = shequcomboBox2.SelectedItem as NameAndGeometry;
            IGeometry geometry = ne.Geometry;
            IEnvelope env = geometry.Envelope;

            double a3width = 29.7 / 2.54 * 96;
            double a3height = 42 / 2.54 * 96;

            mapSpliter.MapSpliter mapspliter = new MapSpliter();
            mapspliter.setActiveView(mapcontrol.ActiveView);
            mapspliter.setGeometry(ne.Geometry);
            string outputPath = "c:\\" + ne.ToString();
            if (!Directory.Exists(outputPath))
                Directory.CreateDirectory(outputPath);
            mapspliter.setOutput(outputPath);
            mapspliter.setPicSize((int)a3width, (int)a3height);
            mapspliter.setStartPoint(env.XMin, env.YMax);
            mapspliter.setOverlapRatio(double.Parse(ConfigurationManager.AppSettings["交叠度"]));
            double[] scales = new double[] { mapcontrol.MapScale };
            mapspliter.splitMap(scales, env.XMin, env.YMin, env.XMax, env.YMax);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (jiedaocomboBox1.SelectedItem == null)
            {
                MessageBox.Show("请选择街道");
                return;
            }
            NameAndGeometry ne = jiedaocomboBox1.SelectedItem as NameAndGeometry;
            mapcontrol.Extent = ne.Geometry.Envelope;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (shequcomboBox2.SelectedItem == null)
            {
                MessageBox.Show("请选择社区");
                return;
            }
            NameAndGeometry ne = shequcomboBox2.SelectedItem as NameAndGeometry;
            mapcontrol.Extent = ne.Geometry.Envelope;
        }

        private void PrintForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }
    }
}